//
//  HomeTableViewController.swift
//  SinaWeibo
//
//  Created by 周小伟 on 16/8/21.
//  Copyright © 2016年 周小伟. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

private let HomeCellId = "HomeCellId"

class HomeTableViewController: BaseTableViewContreoller {
    
    private lazy var statuses = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin{
            visitorLoginView?.setUIInfo(nil, title: "关注一些人,回这里看看有什么惊喜")
            return
        }
        prepareTableView()
        
        loadData()
        
    }
    
    private func prepareTableView(){
        //设置下拉刷新
        refreshControl = UIRefreshControl()
        
        refreshControl?.addTarget(self, action: #selector(HomeTableViewController.loadData), forControlEvents: .ValueChanged)
        
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellId)
        
        tableView.estimatedRowHeight = 200
        //需要制定自动计算行高
        //要使用自动计算行高，必须有一个自上而下的约束
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.separatorStyle = .None
        
        tableView.tableFooterView = indicatorView
    }
    
    @objc private func loadData(){
        //确定since_id
        var since_id = 0
        var max_id = 0
        if indicatorView.isAnimating(){
            since_id = 0
            max_id = statuses.last?.id ?? 0
        }else{
            max_id = 0
            since_id = statuses.first?.id ?? 0
        }
        
        StatusListViewModel.loadData(since_id: since_id, max_id: max_id) { (array) in
            print(array?.count)
            self.refreshControl?.endRefreshing()
            
            if array == nil{
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                self.indicatorView.stopAnimating()
                return
            }
            
            if since_id > 0{
                self.statuses = array! + self.statuses
            }else if max_id > 0{
                self.statuses += array!
                self.indicatorView.stopAnimating()
            }else{
                self.statuses = array!
            }
            
            self.tableView.reloadData()
        }
    }
    
    
    private lazy var indicatorView:UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
        return indicator
    }()
}

extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatusCell
        let status = statuses[indexPath.row]
        cell.status = status
        
        if indexPath.row == statuses.count - 1 && !indicatorView.isAnimating(){
            indicatorView.startAnimating()
            loadData()
            print("开始静默加载数据")
        }
        return cell
    }
}
